function ChaJian() {
}
/**
 * 获取文件路径
 * @param id jquery选择权
 * @param func 回调
 */
ChaJian.prototype.getFilePath = function (id,func) {
    var file = $(id).get(0).files;
    var read = '';
    for(var a=0;a<file.length;a++){
        read = new FileReader();
        read.readAsDataURL(file[a]);
        read.onloadend = function () {
            var src = this.result;
            func(src);
        }
    }
}
/**
 * form 表单提交数据
 * @param id jquery选择权
 * @param url api接口
 * @param success 成功回调
 * @param error 失败回调
 * @param async 是否异步
 * @constructor
 */
ChaJian.prototype.formToAjax = function (id,url,success,error,async) {
    var type = false;
    if(!async){
        type = false;
    }else{
        type = async;
    }
    id.attr('enctype','multipart/form-data');
    var $data=new FormData(id[0]);
    $.ajax({
        url: url,
        type: 'POST',
        cache: false,
        async:type,
        data: $data,
        processData: false,
        contentType: false,
        success:success,
        error:error
    });
}
/**
 * 获取多选框数据
 * @param id juqery选择器
 * @param attribute 是否获取属性
 * @returns {Array}
 */
ChaJian.prototype.getCheckBox = function (id,attribute) {
    var array = [];
    $.each(id,function (i,v) {
        if($(v).prop('checked')){
            if(parseInt($(v).val())){
                if(attribute){
                    array.push($(v).attr(attribute));
                }else{
                    array.push($(v).val());
                }
            }
        }
    });
    return array;
}
/**
 * 上传文件
 * @param id jquery选择权
 * @param url api接口
 * @param success 成功回调
 * @param error 失败回调
 * @param name 上传名称
 * @param type 上传类型
 * @param async 是否异步
 */
ChaJian.prototype.upFile = function (id,url,success,error,name,type,async) {
    if(!type){
        type = "POST";
    }
    if(!async){
        async = true;
    }
    if(!name){
        name = 'file';
    }
    var file = id.get(0).files;
    var formdata = new FormData();
    $.each(file,function (i,v) {
        // console.log(v)
        formdata.append(name+i,v);
    })
    console.log(formdata)
    $.ajax({
        url:url,
        type:type,
        async:async,
        data:formdata,
        contentType: false, //不设置内容类型
        processData: false,
        success:function (res) {
            if(success){
                success(res);
            }
        },
        error:function (res) {
            if(error){
                error(res)
            }
        }
    })
}
/**
 * 分片上传
 * @param id jquery选择器
 * @param url api接口
 * @param quanbu 全上传回调
 * @param yige 一个完成回调
 * @param yipian 一片上传回调
 * @param maxsize 每片的大小
 * @param key 是否自定义key，不填为file
 */
ChaJian.prototype.fenpianFile = function (id,url,quanbu,yige,yipian,maxsize,key) {
    this.file = id.get(0).files;
    this.url = url;
    var that = this;
    this.reult = [];
    // this.binfa = 3;
    // this.newbinfa = 0;
    if(!maxsize){
        maxsize = 5242880;
    }
    this.maxsize = maxsize;
    // if(!async){
    async = false;
    // }
    this.async = async;
    if(quanbu){
        this.quanbu = quanbu;
    }
    if(yige){
        this.yige = yige;
    }
    if(!key){
        this.key = 'file';
    }else{
        this.key = key;
    }
    if(yipian){
        this.yipian = yipian;
    }
    this.hebin = function (res) {
        // console.log(res);
        $.post(that.url,res,function (data) {
            console.log('合并一次');
            if(data.wc){
                if(that.yige){
                    that.reult.push(data);
                    that.yige(data)
                }
                return;
            }else{
                that.hebin(data);
            }
        })
    }
    this.fenpian = function (file,name,index,number,start) {
        index++;
        if(!start){
            start = 0;
        }
        var end = start+that.maxsize;
        if(end>file.size){
            end = file.size;
        }
        var chunk = file.slice(start,end);
        var formdata = new FormData();
        formdata.append(that.key,chunk,name);
        formdata.append('name',name);
        formdata.append('index',index);
        formdata.append('number',number);
        $.ajax({
            url:that.url,
            type:'post',
            async:true,
            data:formdata,
            contentType: false, //不设置内容类型
            processData: false,
            success:function (res) {
                // console.log(res);
                if(that.yipian){
                    that.yipian(res);
                }
                if(res.success==1){
                    res.hebin = true;
                    that.hebin(res);
                }else{
                    that.fenpian(file,name,index,number,end);
                }
            }
        })
    }
    this.onse = function (file) {
        var number = Math.ceil(file.size/that.maxsize);
        var name = file.name;
        that.fenpian(file,name,0,number);
    }
    $.each(this.file,function (i,v) {
        that.onse(v);
    })
    // if(that.quanbu){
    //     that.quanbu(that.reult);
    // }
    var xunhuan = setInterval(function () {
        if(that.reult.length==that.file.length){
            if(that.quanbu){
                that.quanbu(that.reult);
                clearInterval(xunhuan);
            }
        }
    },500);
}
/**
 * 添加数组
 * @param array1
 * @param array2
 * @returns {*}
 */
ChaJian.prototype.addArray = function (array1,array2) {
    $.each(array2,function (i,v) {
        array1.push(v);
    })
    return array1;
}
/**
 * 删除数组元素
 * @param array1
 * @param array2
 * @returns {Array}
 */
ChaJian.prototype.delArray = function (array1,array2){
    var array3 = [];
    var bool ='';
    $.each(array1,function (i,v) {
        bool = true;
        $.each(array2,function(is,vs){
            if(v==vs){
                bool = false;
            }
        })
        if(bool){
            array3.push(v);
        }
    })
    return array3;
}
/**
 * 获取vue的
 * @param 获取数据的url url
 * @param vue的id id
 * @param 条件 tiaojian
 * @param 分页的地址 page
 * @param vue的方法{} func
 */
ChaJian.prototype.getvue = function (url,id,tiaojian,page,func) {
    if(!func){
        func = {};
    }
    $.post(url,tiaojian,function (res) {
        if(res.code==200){
            var data = res.data;
            var laypage = layui.laypage;
            var vues = new Vue({
                el:id,
                data:{
                    shuju:data.data
                },
                methods:func
            })
            laypage.render({
                elem: page //注意，这里的 test1 是 ID，不用加 # 号
                ,count: parseInt(data.total)//数据总数，从服务端得到
                ,limit:parseInt(data.per_page)
                ,jump:function (obj,first) {
                    if(!first){
                        tiaojian.page = obj.curr;
                        $.post(url,tiaojian,function (ress) {
                            if(ress.code==200){
                                vues.shuju = ress.data.data;
                            }else{
                                layer.msg(ress.msg);
                            }
                        })
                    }
                }
            })
        }else{
            layer.msg(res.msg);
        }
    })
}
/**
 * 倒计时
 * @param thisfunc
 * @param number
 * @constructor
 */
ChaJian.prototype.DiaoJiSi = function (thisfunc,number) {
    this.number = 60+1;
    if(number){
        this.number = number+1;
    }
    if(thisfunc){
        this.thisfunc =thisfunc;
    }
    var that = this;
    this.shijian = setInterval(function () {
        that.number--;
        that.thisfunc(that.number);
        if(that.number==0){
            clearInterval(that.shijian);
        }
    },1000);
}
/**
 * 七牛云上传
 * @param url
 * @param id
 * @param func
 * @param number
 * @constructor
 */
ChaJian.prototype.QiNiuUpfile = function (url,id,func,number) {
    var that = this;
    if(!number){
        number = 0;
    }
    this.number = number;
    this.file = id.get(0).files;
    this.files = id.get(0).files[number];
    this.id = id;
    if(!this.files){
        return;
    }
    if(!this.reult){
        this.reult = [];
    }
    this.url = url;
    var yuanname = this.files.name;

    this.new_name ='';
    this.func = func;
    this.observer = {
        next(res){
            if(that.func.next){
                that.func.next(res);
            }
        },
        error(err){
            if(that.func.error){
                that.func.error(res);
            }
        },
        complete(res){
            // console.log(res);
            res.yuan_name = that.new_name;
            $.post(that.url,res,function (ress) {
                if(that.func.complete){
                    that.reult.push(ress);
                    that.func.complete(ress);
                    if(that.file.length==that.reult.length){
                        if(that.func.wc){
                            that.func.wc(that.reult);
                        }
                    }
                }
                ChaJian.QiNiuUpfile(that.url,that.id,that.func,that.number+1);
            })

        }
    };
    this.token = '';
    // console.log(this.observer);
    this.update = function(){
        var observable  = qiniu.upload(that.files,that.new_name,that.token);
        var subscription = observable.subscribe(that.observer);

    }
    $.post(that.url,{name:yuanname},function (res) {
        console.log(res);
        that.new_name = res.name;
        that.token = res.token;
        that.update()

    })

}
var ChaJian = new ChaJian();